#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> pii;
int dist[N];
bool st[N];
priority_queue<pii, vector<pii>, greater<pii>> q;
int n, m, s;
vector<pii> edges[N];
void dijiesila()
{
    memset(dist, 0x3f, sizeof(dist));
    dist[s] = 0;
    q.push({ 0,s });
    while (q.size())
    {
        auto e = q.top();
        q.pop();
        int a = e.first;
        int b = e.second;
        if (st[b])
        {
            continue;
        }
        st[b] = true;
        for (auto r : edges[b])
        {
            int u = r.first;
            int w = r.second;
            if (dist[b] + w < dist[u])
            {
                dist[u] = dist[b] + w;
                q.push({ dist[u],u });
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
}
int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edges[u].push_back({ v,w });
    }
    dijiesila();
    return 0;
}